/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.mobiwebinc.compconn.communication;

import com.mobiwebinc.compconn.client.ClientHandler;
import com.mobiwebinc.compconn.client.ClientList;
import com.mobiwebinc.compconn.configuration.Constant;
import java.util.Collection;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author suraj
 */
public class ChannelCleaner extends Thread {
    ClientList clientList;
    boolean running;
    
    public ChannelCleaner() {
        clientList = ClientList.getInstance();
        running = true;
    }

    public void done() {
        running = false;
    }
    
    @Override
    public void run() {
        while (running) {
            try {
                sleep(Constant.CLEAN_TIME_INTERVAL);
            } catch (InterruptedException ex) {
                Logger.getLogger(ChannelCleaner.class.getName()).log(Level.SEVERE, null, ex);
            }
            clean();
        }
    }

    private void clean() {
        Collection<ClientHandler> handlers = clientList.getHandlers().values();
        for(ClientHandler handler: handlers) {
            handler.clean();
        }
    }
}
